<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:10
 */

namespace App\Content\Controllers\Home;

use App\Base\Controllers\Controller;
use App\Content\Contracts\WxUsersInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;

class WxUserController  extends Controller
{
    /**
     * 获取用户登陆
     * @return WxUsersInterface
     */

    public function getWxUsersInterface(){
        return app()->make(WxUsersInterface::class);
    }

    /**
     * 第三方登陆
     * @param Request $request
     * @throws \App\Base\Exceptions\ApiException
     */
    public function login(Request $request)
    {
        $validator = Validator::make($request->except('data'), [
            'openid' => 'required',
            'nickname' => 'required',
            'sex' => 'required',
            'city'=>'required',
            'login_type' => 'required',
            'province'=>'required',
            'headimgurl'=>'required',
        ]);
        if ($validator->fails()) {

            $this->throwValidatorException($validator);
        }

        return $this->getWxUsersInterface()->thridLogin($request->except("data"));


     }

    /**
     * 手机登陆
     * @throws \App\Base\Exceptions\ApiException
     */
     public function mobileLogin(Request $request){

         $validator = Validator::make($request->all(), [
             'mobile' => 'required|regex:/^1[345789]\d{9}$/|',
             'password' => 'required'
         ]);
         if ($validator->fails()) {
             $this->throwValidatorException($validator);
         }
         return $this->getWxUsersInterface()->mobileLogin($request->except("data"));
     }

    /**
     * 手机注册
     * @param Request $request
     */
     public function mobileReg(Request $request){

         $validator = Validator::make($request->all(), [
             'mobile' => 'required|regex:/^1[345789]\d{9}$/',
             'code' => 'required',
             'password' => 'required',

         ]);

         if ($validator->fails()) {
             $this->throwValidatorException($validator);
         }

         return $this->getWxUsersInterface()->mobileReg($request->except("data"));
     }

    /**
     * 发送手机短信
     * @param Request $request
     * @throws \App\Base\Exceptions\ApiException
     */
     public function sendSmsCode(Request $request){
         $validator = Validator::make($request->all(), [
             'mobile' => 'required|regex:/^1[345789]\d{9}$/',
         ]);
         if ($validator->fails()) {
             $this->throwValidatorException($validator);
         }
         return $this->getWxUsersInterface()->sendSmsCode($request->except("data"));
     }

     public function getUserInfo(Request $request){


         $where=function($query) {
              $query->where("uid",AUID);
         };
         return $this->getWxUsersInterface()->getUserInfo($where);
     }


     public function getUserDetail(Request $request){
         $validator = Validator::make($request->all(), [
             'uid' => 'required',
         ]);
         if ($validator->fails()) {
             $this->throwValidatorException($validator);
         }
         $uid=$request->input("uid");
         $where=function($query) use($uid){
             $query->where("uid",$uid);
         };


         $token=$request->input("token");
         if(!empty($token)){
             $string=base64_decode($token);
             $arr=json_decode($string,true);
             $key=$arr["token"];
             $mdKey=Cache::get($arr['uid']);
             if($key==$mdKey){
                 define("AUID",$arr['uid']);
             }
         }

         return $this->getWxUsersInterface()->getUserInfo($where);
     }

    /**
     * 修改用户昵称
     * @param Request $request
     */
     public function updateUserNickname(Request $request){
        return $this->getWxUsersInterface()->updateUser($request->except("data","token"));
     }

    /**
     * 修改用户头像
     * @param Request $request
     */
    public function updateUserHeadurl(Request $request){
        return $this->getWxUsersInterface()->updateUser($request->except("data","token"));
    }

    /**
     * 修改密码
     */
    public function updateUserPassword(Request $request){
        $validator = Validator::make($request->all(), [
            'mobile' => 'required|regex:/^1[345789]\d{9}$/',
            'code' => 'required',
            'password' => 'required',
        ]);
        if ($validator->fails()) {
            $this->throwValidatorException($validator);
        }
        return $this->getWxUsersInterface()->updateUserPassword($request->except("data","token"));
    }


    /**
     * 获取我的收藏
     * @param Request $request
     */
     public function getUserMyCollection(Request $request){
         $where=function($query){

         };
        return $this->getWxUsersInterface()->getUserMyCollection($where);
     }

    /**
     * 获取我的评论
     * @param Request $request
     */
     public function getUserMyComment(Request $request){
         $where=function($query){
            $query->where("user_id",AUID);
         };
         return  $this->getWxUsersInterface()->getUserMyComment($where);
     }
    /**
     * 测试请求
     * @param Request $request
     *
     */
     public function testRequest(Request $request){

        echo  base64_encode(json_encode($request->all()));
        exit;
     }
}